package com.sjq.common.web.api;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.service.MainService;
import com.sjq.common.web.SessionUserHelper;

@Controller
@RequestMapping(value = "/main/web/api")
public class MainRestController {
    
    Logger logger = Logger.getLogger(MainRestController.class);
    
    @Autowired
    private HttpSession session;
    
    //菜单管理 逻辑层
    @Autowired
    private MainService mainService;
    
    /**
     * 获取当前用户
     * @return
     */
    @RequestMapping(value = "/currUser")
    @ResponseBody
    public LoginAccount currUser() {
        List<Map<String, String>> menuList = null;
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount account = helper.getLoginAccount();
        return account;
    }
    
    /**
     * 获取子菜单
     * @param pid 父菜单
     * @return
     */
    @RequestMapping(value = "/subMenu")
    @ResponseBody
    public List<Map<String, String>> subMenu(String pid) {
        List<Map<String, String>> menuList = null;
        try {
            SessionUserHelper helper = SessionUserHelper.createBySession(session);
            String roledm = helper.getLoginAccount().getUserRole();
            menuList = mainService.loadSubMenu(roledm, pid);
        } catch (SQLException e) {
            logger.error("获取子菜单异常，菜单id["+pid+"]",e);
        }
        return menuList;
    }
    
    /**
     * 获取子菜单<br/>
     * 获取后代所有子菜单
     * @param pid 父菜单
     * @return
     */
    @RequestMapping(value = "/allSubMenu")
    @ResponseBody
    public List<Map<String, Object>> allSubMenu(String pid) {
        List<Map<String, Object>> menuList = null;
        try {
            SessionUserHelper helper = SessionUserHelper.createBySession(session);
            String roledm = helper.getLoginAccount().getUserRole();
            menuList = mainService.loadAllSubMenu(roledm, pid);
        } catch (SQLException e) {
            logger.error("获取子菜单异常，菜单id["+pid+"]",e);
        }
        return menuList;
    }
}
